package com.ruoyi.base.controller;

import com.ruoyi.base.domain.SjzMaterials;
import com.ruoyi.base.domain.SjzTouchRecord;
import com.ruoyi.base.domain.Touch;
import com.ruoyi.base.domain.TouchVo;
import com.ruoyi.base.service.ISjzMaterialsService;
import com.ruoyi.base.service.ISjzTouchRecordService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class ApiController {
    @Autowired
    private ISjzMaterialsService sjzMaterialsService;
    @Autowired
    private ISjzTouchRecordService sjzTouchRecordService;

    @Anonymous
    @GetMapping("/touch")
//    @Log(title = "属鼠摸金", businessType = BusinessType.OTHER)
    public AjaxResult list(HttpServletRequest request)
    {
        List<TouchVo> touchVos = sjzMaterialsService.touchMet();
        List<Touch> list = touchVos.get(0).getList();
        // 获取设备信息
        // 获取User-Agent头部
        String userAgent = request.getHeader("User-Agent");
        String ip = IpUtils.getIpAddr(request);

//        String ids = list.stream()
//                .map(touch -> String.valueOf(touch.getId()))  // 提取id并转为字符串
//                .collect(Collectors.joining(","));
        String names = list.stream()
                .map(Touch::getName)  // 提取id并转为字符串
                .collect(Collectors.joining(","));
        sjzTouchRecordService.insertSjzTouchRecord(new SjzTouchRecord(userAgent,ip,"",names));
        // 获取
        return AjaxResult.success(touchVos);
    }
}
